package com.mrogrape.notepad.ui.library;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.mrogrape.notepad.common.ViewUtils;
import com.mrogrape.notepad.data.ViewContext;
import com.mrogrape.notepad.ui.filechooser.NotepadFileChooser;

/**
 * 用来编辑库包含对象的对话框。
 * 
 * @author IceSpring
 *
 */
public class InclusionDialog extends JDialog {

	public static final int APPROVE_OPTION = 1;
	public static final int CANCEL_OPTION = 0;

	/**
	 * 创建对话框
	 * @param frame
	 */
	public InclusionDialog(Frame frame) {

		super(frame, true);

		this.initComponents();
	}

	/**
	 * 显示对话框并指定名称和链接，如果指定为null则不做变更。
	 * 
	 * <p>
	 * 此方法会在显示后阻塞，知道用户做出可以关闭对话框的相应。
	 * 
	 * @param title 标题
	 * @param name 导入对象的名称
	 * @param href 导入对象的链接
	 * @return 用户的操作结果
	 */
	public int showDialog(String title, String name, String href) {

		if(title != null){
			this.setTitle(title);
		}

		if(name != null){
			this.nameText.setText(name);
		}

		if(href != null){
			this.hrefText.setText(href);
		}

		this.setVisible(true);

		return this.option;
	}

	/**
	 * 设置显示的名称
	 * 
	 * @param name
	 */
	public void setInclusionName(String name) {

		this.nameText.setText(name);
	}


	/**
	 * 获取名称。
	 * 
	 * @return
	 */
	public String getInclusionName() {

		return this.nameText.getText();
	}


	/**
	 * 设置导入对象的位置。
	 * 
	 * @param location
	 */
	public void setInclusionLocation(String location){

		this.hrefText.setText(location);
	}

	/**
	 * 获取导入对象的位置。
	 * 
	 * @return
	 */
	public String getInclusionLocation(){

		return this.hrefText.getText();
	}


	private void initComponents() {

		JLabel nameLabel = new JLabel("Name: ");
		JLabel hrefLabel = new JLabel("Location: ");

		JButton browseBtn = new JButton("Browse");
		browseBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				InclusionDialog.this.browseBtnActionPerformed(arg0);
			}
		});

		JButton cancelBtn = new JButton("Cancel");
		cancelBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				InclusionDialog.this.cancelBtnActionPerformed(arg0);
			}
		});

		JButton approveBtn = new JButton("Approve");
		approveBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				InclusionDialog.this.approveBtnActionPerformed(arg0);
			}
		});
		GroupLayout groupLayout = new GroupLayout(this.getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addContainerGap().addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup().addComponent(approveBtn).addPreferredGap(ComponentPlacement.RELATED).addComponent(cancelBtn)).addGroup(groupLayout.createSequentialGroup().addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addComponent(hrefLabel).addComponent(nameLabel)).addPreferredGap(ComponentPlacement.RELATED).addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addComponent(this.nameText, GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE).addGroup(groupLayout.createSequentialGroup().addComponent(this.hrefText, GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE).addPreferredGap(ComponentPlacement.RELATED).addComponent(browseBtn))))).addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addContainerGap().addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(nameLabel).addComponent(this.nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addPreferredGap(ComponentPlacement.UNRELATED).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(hrefLabel).addComponent(this.hrefText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(browseBtn)).addPreferredGap(ComponentPlacement.RELATED, 34, Short.MAX_VALUE).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(cancelBtn).addComponent(approveBtn)).addContainerGap()));
		this.getContentPane().setLayout(groupLayout);
		this.pack();

		this.setLocationRelativeTo(super.getOwner());
	}

	protected void browseBtnActionPerformed(ActionEvent arg0) {

		NotepadFileChooser chooser = ViewContext.getFileChooser();

		int srcMode = chooser.getFileSelectionMode();
		chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

		File selFile = null;

		if(!LibraryUtils.isUrlString(this.hrefText.getText())){
			selFile = new File(this.hrefText.getText());
		}
		chooser.setSelectedFile(selFile);

		int result = chooser.showOpenDialog(this);
		if (result == JFileChooser.APPROVE_OPTION) {
			this.hrefText.setText(chooser.getSelectedFile().getAbsolutePath());
		}

		chooser.setFileSelectionMode(srcMode);
	}

	protected void approveBtnActionPerformed(ActionEvent arg0) {

		String name = this.nameText.getText();
		String path = this.hrefText.getText();
		if (name.isEmpty()) {
			ViewUtils.showErrorMessageDialog(this, "Name can not be empty");
			return;
		}
		if (path.isEmpty()) {
			ViewUtils.showErrorMessageDialog(this, "object location can not be empty");
			return;
		}

		this.option = InclusionDialog.APPROVE_OPTION;
		this.setVisible(false);
	}

	protected void cancelBtnActionPerformed(ActionEvent arg0) {

		this.option = InclusionDialog.CANCEL_OPTION;
		this.setVisible(false);
	}

	private JTextField nameText = new JTextField();
	private JTextField hrefText = new JTextField();

	private int option;
}
